﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.LIB;
using ModelChecking.ModelValidation.Utils;

namespace ModelChecking.ModelValidation.FireCompartment
{
	public partial class DlgGetDistance : System.Windows.Forms.Form
	{
		public DlgGetDistance(string inputResultFormName = null, string inputDataPath = null)
		{
			this.InitializeComponent();
			//base.Icon = Resource.YArchitechIcon;
			if (inputResultFormName != null)
			{
				this.Text = inputResultFormName;
			}
			this.m_nowUseDataPath = inputDataPath;
		}

		private void DlgGetDistance_Load(object sender, EventArgs e)
		{
			this.ReadFormData();
		}

		public void ResetResult(double distance)
		{
			this.m_dLastDist += distance;
			this.lbResult.Text = this.m_dLastDist.ToString("#0.00") + " m";
			this.tbDimText.Text = this.m_dLastDist.ToString("#0.00") + " m";
			double num = this.m_dLastDist * 1.25;
			this.lbAddedResult.Text = num.ToString("#0.00") + " m";
		}

		public bool DimOrNot()
		{
			return this.cbDim.Checked;
		}

		public string DimText()
		{
			return this.tbDimText.Text;
		}

		private void ReadFormData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "DistanceMeasurement.dat";
			if (!File.Exists(path))
			{
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = 0;
			int y = 0;
			try
			{
				x = Convert.ToInt32(streamReader.ReadLine());
				y = Convert.ToInt32(streamReader.ReadLine());
				this.cbDim.Checked = Convert.ToBoolean(streamReader.ReadLine());
			}
			catch
			{
				x = 0;
				y = 0;
				this.cbDim.Checked = true;
			}
			streamReader.Close();
			fileStream.Close();
			Point location = new Point(x, y);
			base.Location = location;
		}

		private void WriteFormData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "DistanceMeasurement.dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.cbDim.Checked);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void DlgGetDistance_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(Cursor.Position))
			{
				int wMsg = 7;
				DlgGetDistance.SendMessage(GetDistanceTempleCmd.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				this.WriteFormData();
				return;
			}
			base.Focus();
		}

		private void DlgGetDistance_FormClosed(object sender, FormClosedEventArgs e)
		{
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			DlgGetDistance.SendMessage(GetDistanceTempleCmd.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			DlgGetDistance.keybd_event(bVk, 0, dwFlags, 0);
			DlgGetDistance.keybd_event(bVk, 0, dwFlags2, 0);
			this.WriteFormData();
		}

		private void DlgGetDistance_Leave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(Cursor.Position))
			{
				int wMsg = 7;
				DlgGetDistance.SendMessage(GetDistanceTempleCmd.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				this.WriteFormData();
				return;
			}
			base.Focus();
		}

		private void btnRules_Click(object sender, EventArgs e)
		{
			DlgDistanceRules dlgDistanceRules = new DlgDistanceRules(this.m_nowUseDataPath);
			dlgDistanceRules.ShowDialog();
		}

		private void cbDim_CheckedChanged(object sender, EventArgs e)
		{
			this.tbDimText.Enabled = this.cbDim.Checked;
		}

		private double m_dLastDist;

		private string m_nowUseDataPath;
	}
}
